import { Injectable } from '@angular/core';
import { User } from '../../@shared/models/user';

const USERS = [
  {
    gender: 'male',
    userName: 'Admin',
    account: 'admin',
    password: 'admin',
    phoneNumber: '19999996666',
    email: 'admin@sina.com',
    userId: '100',
  },
];

@Injectable()
export class AuthService {
  constructor() {}

  login(account: string, password: string): Promise<User> {
    return new Promise<User>((resolve, reject) => {
      let user: User = null;
      for (let i = 0; i < USERS.length; i++) {
        const { account: _account, password: _password, userName, gender, email, phoneNumber } = USERS[i];
        if (account === _account && password === _password) {
          user = { userName, gender, phoneNumber, email };
          break;
        }
      }
      resolve(user);
    });

    // return throwError('Please make sure you have input correct account and password');
  }

  logout() {
    localStorage.removeItem('id_token');
    localStorage.removeItem('expires_at');
    localStorage.removeItem('userinfo');
  }

  setSession(user: User) {
    localStorage.setItem('id_token', '123456');
    localStorage.setItem('userinfo', JSON.stringify(user));
    localStorage.setItem('expires_at', '120');
  }

  isUserLoggedIn() {
    if (localStorage.getItem('userinfo')) {
      return true;
    } else {
      return false;
    }
  }
}
